/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mongoosehome.web.login.dao;

import com.mongoosehome.web.common.domain.TUser;
import com.mongoosehome.web.util.HibernateUtils;
import com.mongoosehome.web.util.PageInfo;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Administrator
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{    
    static Logger log = Logger.getLogger(UserDaoImpl.class);
    @Override
    public void saveUser(TUser user) {
        this.getHibernateTemplate().save(user);
    }

    @Override
    public void updateUser(TUser user) {
        this.getHibernateTemplate().update(user);
    }

    @Override
    public TUser getUser(int userid) {
        return   (TUser)this.getHibernateTemplate().get(TUser.class, userid);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<TUser> getUserListByEmail(String email) {
        String hql = "from TUser aa where aa.email=?";
        return   this.getHibernateTemplate().find(hql,new Object[]{email});
    }

    @Override
    public TUser getUserByUserName(String userName) {
        String hql = "from TUser u where u.nickName = ?";
        List<TUser> list = (List<TUser>)this.getHibernateTemplate().find(hql, userName);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null; 
    }

    public PageInfo getUserByPage(int pageNo,int maxResult) {
        StringBuilder hql = new StringBuilder(" from TUser u " );  
        
        return HibernateUtils.getNumberPage(this.getSession(), this.getSessionFactory(),pageNo, maxResult, hql.toString(),null,false);
    }
    
}
